import os, thread
import time
from postProccesing import postProccesor

class Experiment:
    
    def __init__(self,name, desc, runs, outdir,res):
        self.name = name
        self.desc = desc
        self.runs = runs
        t = [str(s) for s in time.localtime()]
        self.startTime = "-".join(t[1:3])+"_"+ ":".join(t[3:6])
        self.outdir = outdir + "/"+ self.name + self.startTime
        self.status = 0
        self.resources = res 
        if(not os.path.isdir(self.outdir)):
           os.makedirs(self.outdir, 0777)
        print
        print "Experiment '" + self.name + "' in ."+ self.outdir.replace(outdir,"")
    
    def letsGo(self):
        self.status = 1
        print "Running experiment " + self.name
        self.stopTime = 'Still running!'
        i=0
        for r in self.runs:
            dir  = self.outdir + "/" + r.algo.name + "_" + str(os.path.basename(r.pars['trainfile:']))
            dir = dir.replace(".dataset","") + "_" +str(i)
            if(not os.path.isdir(dir)):
               os.makedirs(dir, 0777)
            r.runMe(self.resources.rlist[0],dir)
            i += 1
        t = [str(s) for s in time.localtime()]
        self.stopTime = "-".join(t[1:3])+"_"+ ":".join(t[3:6])
        postProccesor(self.outdir).doExperimentReport(self)
        self.status = 0
        